package logic

import (
	"center/centerrpc/centerrpc"
	"center/centerrpc/internal/svc"
	"center/models/mysql"
	"context"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealLogic {
	return &RealLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RealLogic) Real(in *centerrpc.RealRequest) (*centerrpc.RealResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, errors.New("当前用户不存在")
	}

	var reals mysql.UserReal
	mysql.DB.Debug().Where("id = ?", in.UserId).Find(&reals)

	if reals.UserId == 0 {
		return nil, errors.New("当前用户未实名")
	}

	reals = mysql.UserReal{
		UserId: int(in.UserId),
		Card:   in.Card,
		Name:   in.Name,
	}
	mysql.DB.Debug().Updates(&reals)

	return &centerrpc.RealResponse{
		UserId: int64(user.ID),
	}, nil
}
